/**
 * Copyright (C) 2014-2016, Hrfax and/or its affiliates. All rights reserved.
 * Hrfax PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package cn.zs.supply.common.core.login;
import cn.zs.supply.common.core.login.domain.LoginInfo;
import cn.zs.supply.common.core.login.domain.Role;
import java.util.HashSet;
import java.util.List;
public class LoginContext {

    private static ThreadLocal<LoginInfo> loginInfoHolder = new ThreadLocal<>();

    public static LoginInfo get() {
        LoginInfo loginInfo=loginInfoHolder.get();
        if(loginInfo!=null){
            return loginInfoHolder.get();
        }
        else{
            loginInfo=new LoginInfo();
            loginInfoHolder.set(loginInfo);
            return loginInfoHolder.get();
        }
    }
    public static void remove() {
        loginInfoHolder.remove();
    }

    public static void put(LoginInfo loginInfo) {
        loginInfoHolder.set(loginInfo);
    }

    public static Integer getUserId() {
        LoginInfo loginInfo = get();
        return loginInfo == null ? null : loginInfo.getId();
    }

    public static String getUserName() {
        LoginInfo loginInfo = get();
        return loginInfo == null ? null : loginInfo.getName();
    }

    public static Integer getOrgType() {
        LoginInfo loginInfo = get();
        return loginInfo == null ? null : loginInfo.getOrgType();
    }

    public static Integer getDeptId() {
        LoginInfo loginInfo = get();
        return loginInfo == null ? null : loginInfo.getDeptId();
    }

    public static String getDeptCode() {
        LoginInfo loginInfo = get();
        return loginInfo == null ? null : loginInfo.getDeptCode();
    }

    public static String getDeptName() {
        LoginInfo loginInfo = get();
        return loginInfo == null ? null : loginInfo.getDept().getName();
    }

    public static String getLoginAgent() {
        LoginInfo loginInfo = get();
        return loginInfo == null ? null : loginInfo.getLoginAgent();
    }


    /**
     * @return
     */
    public static String getAccount() {

        LoginInfo loginInfo = get();
        return loginInfo == null ? null : loginInfo.getAccount();
    }

    public static String getLoginSource() {
        LoginInfo loginInfo = get();
        if (loginInfo == null) {
            return "";
        }
        return loginInfo.getBusiSource();
    }



    /**
     * @return
     */
    public static Integer getOrgId() {
        LoginInfo loginInfo = get();
        if (loginInfo == null) {
            return null;
        }
//		return loginInfo.getOrganization().getId();
        return loginInfo.getDept().getOrgId();
    }

    /**
     * @return
     */
    public static String getRoleName() {
        LoginInfo loginInfo = get();
        if (loginInfo == null) {
            return null;
        }
        //todo 默认取第一个
        Role role = loginInfo.getRoles().get(0);
        return role.getName();
    }

    /**
     * @return
     */
    public static String getPhone() {

        LoginInfo loginInfo = get();
        return loginInfo == null ? null : loginInfo.getPhone1();
    }

    public static List<Long> getRoleIds() {
        LoginInfo loginInfo = get();
        if (loginInfo == null) {
            return null;
        }
        return loginInfo.getRolesIds();
    }

}
